package org.zwy.gradetrack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.UserRole;
import org.zwy.gradetrack.service.UserRoleService;

import java.util.List;

/**
 * 用户角色管理控制器
 */
@RestController
@RequestMapping("/api/user-roles")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 分页查询用户角色列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<Page<UserRole>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Long roleId) {
        return Result.success(userRoleService.pageUserRoles(current, size, userId, roleId));
    }

    /**
     * 为用户分配角色
     */
    @PostMapping("/assign")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Boolean> assignRoles(
            @RequestParam Long userId,
            @RequestBody List<Long> roleIds) {
        try {
            return Result.success(userRoleService.assignRoles(userId, roleIds));
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }

    /**
     * 移除用户的角色
     */
    @DeleteMapping("/remove")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Boolean> removeRoles(
            @RequestParam Long userId,
            @RequestBody List<Long> roleIds) {
        try {
            return Result.success(userRoleService.removeRoles(userId, roleIds));
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }

    /**
     * 获取用户的所有角色
     */
    @GetMapping("/user/{userId}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<List<UserRole>> getUserRoles(@PathVariable Long userId) {
        return Result.success(userRoleService.getUserRoles(userId));
    }

    /**
     * 获取角色的所有用户
     */
    @GetMapping("/role/{roleId}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<List<UserRole>> getRoleUsers(@PathVariable Long roleId) {
        return Result.success(userRoleService.getRoleUsers(roleId));
    }

    /**
     * 检查用户是否拥有指定角色
     */
    @GetMapping("/check")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<Boolean> hasRole(
            @RequestParam Long userId,
            @RequestParam Long roleId) {
        return Result.success(userRoleService.hasRole(userId, roleId));
    }

    /**
     * 批量导入用户角色数据
     */
    @PostMapping("/batch-import")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Void> batchImport(@RequestBody List<UserRole> userRoles) {
        try {
            userRoleService.batchImport(userRoles);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, e.getMessage());
        }
    }
} 